<?php

include_once('baseModel.php');

class Order extends BaseModel {
  public $_table="order";

  public $id;
  public $identifier;
  public $customer_id;
  
  function loadByLimit($limit) {
    $query = "SELECT `" . $this->_table . "`.* FROM `" . $this->_table . "` ";
    $query .= " INNER JOIN `orderStatus` ON (`order`.id = `orderStatus`.order_id) ";
    $query .= " WHERE (`orderStatus`.isStaged =0 AND `orderStatus`.isCheckedOut =0 AND `orderStatus`.isReturned =0) ";
    if($limit) {
      $query .= " limit 0," . $limit;
    }
    //print $query;
    $results = DBHelper::executeQuery($query);
    if(!$results) {
      return;
    }
    // there might be more than one results
    $orders = array();
    
    foreach($results as $result) {
      $this->populateModel($result);
      array_push($orders, clone $this);
    }
    return $orders;
  }
  
  function loadCheckedByLimit($limit) {
    $query = "SELECT `" . $this->_table . "`.* FROM `" . $this->_table . "` ";
    $query .= " INNER JOIN `orderStatus` ON (`order`.id = `orderStatus`.order_id) ";
    $query .= " WHERE (`orderStatus`.isStaged =0 AND `orderStatus`.isCheckedOut =1 AND `orderStatus`.isReturned =0) ";
    if($limit) {
      $query .= " limit 0," . $limit;
    }
    //print $query;
    $results = DBHelper::executeQuery($query);
    if(!$results) {
      return;
    }
    // there might be more than one results
    $orders = array();
    
    foreach($results as $result) {
      $this->populateModel($result);
      array_push($orders, clone $this);
    }
    return $orders;
  }
  
  function loadByIdentifier($id) {
    $query = "SELECT * FROM `" . $this->_table . "` WHERE identifier = " . $id;

    $results = DBHelper::executeQuery($query);
    print_r($results);
    if(!$results) {
      return;
    }
    $this->populateModel($results[0]);
    return $this;
  }
}

?>